using B.S.DataBase.MyProject.Domain.PatientMobileDevice;
using B.S.DataBase.MyProject.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MedicalPCTerminal.Read.API.Appcation.Commands.TroubleCommand;
using System.Security.Claims;

namespace MedicalPCTerminal.Read.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class DoctorController : ControllerBase
    {
        private readonly IMediator mediator;

        public DoctorController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取医生聊天列表
        /// </summary>
        /// <returns>聊天列表</returns>
        [HttpGet("chat-list")]
        public async Task<APIResult<List<ChatListItem>>> GetChatList(CancellationToken cancellationToken)
        {
            var userName = User.FindFirst(ClaimTypes.Name)?.Value;
            var request = new GetChatListItemCommand
            {
                UserName = userName,
                Role = 1 // 1表示医生
            };
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 搜索聊天联系人
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <returns>搜索结果</returns>
        [HttpGet("chat-search")]
        public async Task<APIResult<List<ChatListItem>>> SearchContacts([FromQuery] string keyword, CancellationToken cancellationToken)
        {
            var userName = User.FindFirst(ClaimTypes.Name)?.Value;
            var request = new GetChatListItemCommand
            {
                UserName = userName,
                Role = 1, // 1表示医生
                Keyword = keyword
            };
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取与特定患者的聊天历史
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <returns>聊天历史记录</returns>
        [HttpGet("chat-history/{patientId}")]
        public async Task<APIResult<List<ChatListItem>>> GetChatHistory(string patientId, CancellationToken cancellationToken)
        {
            var userName = User.FindFirst(ClaimTypes.Name)?.Value;
            var request = new GetChatHistoryCommand
            {
                DoctorName = userName,
                PatientId = patientId
            };
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 标记消息为已读
        /// </summary>
        /// <param name="messageId">消息ID</param>
        /// <returns>操作结果</returns>
        [HttpPut("message-read/{messageId}")]
        public async Task<APIResult<bool>> MarkMessageAsRead(int messageId, CancellationToken cancellationToken)
        {
            var request = new MarkMessageAsReadCommand
            {
                MessageId = messageId
            };
            return await mediator.Send(request, cancellationToken);
        }
    }
} 